import { type MaybeRef, computed, isRef } from 'vue'
import { fetcher } from '../fetcher'
import type {
  CreateClassData,
  CreateClassPayload,
  GetClassData,
  GetClassStudentsData,
  PatchClassStudentsData,
  PatchClassStudentsPayload,
  SearchClassesData,
  SearchClassesPayload,
  SearchClassesQueryKey
} from './types'

export * from './composables'
export { classMessage } from './message'
export type { ClassModel } from './types'

export function ApiGetClass(classID: number) {
  return fetcher<GetClassData>(`/classes/${classID}`)
}

export function ApiGetClassStudents(classID: number) {
  return fetcher<GetClassStudentsData>(`/classes/${classID}/students`)
}

export function ApiPatchClassStudents(id: number, payload: PatchClassStudentsPayload) {
  return fetcher<PatchClassStudentsData>(`/classes/${id}/students`, {
    method: 'PATCH',
    body: payload
  })
}

export const KeyClasses = ['get', 'classes'] as const
export function useSearchClassesQueryKey(
  query: MaybeRef<SearchClassesPayload>
): MaybeRef<SearchClassesQueryKey> {
  return isRef(query)
    ? computed(() => [...KeyClasses, query.value])
    : ([...KeyClasses, query] as const)
}
export function ApiSearchClasses(query: SearchClassesPayload) {
  return fetcher<SearchClassesData>('/classes', { query })
}

export function ApiCreateClass(body: CreateClassPayload) {
  return fetcher<CreateClassData>('/classes', { method: 'POST', body })
}
